package com.cloudcc.cloudframe;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;

import android.app.Activity;
import android.app.Application;
import android.app.Notification;
import android.app.NotificationManager;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.PixelFormat;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.StrictMode;
import android.view.Gravity;
import android.view.WindowManager;
import android.view.WindowManager.LayoutParams;
import android.widget.TextView;

import com.cloudcc.cloudframe.model.Album;
import com.cloudcc.cloudframe.model.UserModel;
import com.cloudcc.cloudframe.util.DensityUtils;
import com.nostra13.universalimageloader.cache.disc.impl.UnlimitedDiscCache;
import com.nostra13.universalimageloader.cache.disc.naming.Md5FileNameGenerator;
import com.nostra13.universalimageloader.cache.memory.impl.UsingFreqLimitedMemoryCache;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.QueueProcessingType;
import com.nostra13.universalimageloader.core.download.BaseImageDownloader;
import com.nostra13.universalimageloader.utils.StorageUtils;

/**
 * 全局应用程序控制
 * <hr>
 * 1、消息推送初始化(百度)<br>
 * 2、地图初始化(百度) 3、异常处理初始化
 * <hr>
 * 
 * @author wangzhiqiang
 * @date 2015-6-15 上午9:56:28
 * 
 */
public class CApplication extends Application {

	public static String urlString = "https://login.cloudcc.com/weixinlogin.action";

	// 定位参数
	public boolean m_bKeyRight = true;
	public static String QueryType = "";
	// 容器
	public static ArrayList<Album> albums;

	// 查询我所追随的内容的条件
	public static final int limit = 10;// 每次加载几条
	// public static final int skip=0;//从第几条开始加载
	public static final String feedSort = "lastModifyDate";
	// public static final String feedSort="createDate";
	public static String ADDMICROPOST = "addMicroPostF";// 默认为发表普通类型的帖子
	public static String ADDMICCOMMENT = "addMicroComment";// 默认发表的普通类型的评论(回复)
	public static String record_id = null;// 小网页 事件详情中点击记录 右面刷新
	// 关注广播
	// public static final String USERINFO_GUANZHU="userinfoguanzhu";
	// 首页中喜欢，留言，赞，删除 广播
	public static final String INDEXADAPTER_LIUYAN = "liuyan";
	public static final String INDEXADAPTER_SHOUCANG = "shoucang";
	public static final String INDEXADAPTER_XIHUAN = "xihuan";
	public static final String INDEXADAPTER_SHANCHU = "shanchu";
	public static final String CHAT_USER_INFO = "chat_user_info";
	public static final String INDEXADAPTER_SHUAXIN = "index_shuaxin";
	public static final String ADDMICRO_COMMENT_SHUAXIN = "comment_shaxin";
	public static final String RECORD_ID_CHANGED_SHUAXIN = "record_changed_shuaxin";
	// chatter首页中 加载更多的计数
	public static int count = 0;
	public static boolean islike;// 是否喜欢
	public static int isclieck;// 当前所选的位置
	public static boolean isshoucang;// 是否收藏
	public static boolean iszhunsui;// 是否追随

	public static int screenW;
	public static int screenH;
	public static boolean islocation = true;// 是否定位，默认是定位
	public static String time = "5分钟";// 上报时间设置
	public static long locationtime = 1200000;// 默认是20分钟定位
	public static boolean isStopLocation = false;

	// 消息推送
	public static boolean islogining = false;// 消息判别是否登录了，登录的就直接进入界面，没登录自动登陆

	// 广播
	@Deprecated
	public static final String USERINFO_GUANZHU = "uderinfo_guanzhu";
	public static final String MESSAGE_SYSTEM = "MESSAGE_SYSTEM";// 系统消息
	public static final String MESSAGE_SYSTEM_NUM = "MESSAGE_SYSTEM_NUM";// 系统消息
	public static final String MESSAGE_DAISHENPI = "MESSAGE_DAISHENPI";// 待审批广播
	public static final String MESSAGE_DAIBANRENWU = "MESSAGE_DAIBANRENWU";// 任务广播
	public static final String SHEBEI_ZHUCE = "SHEBEI_ZHUCE";// 设备注册
	public static final String UPDATE_USER_ICON = "update_user_icon";// 修改成功图片广播
	public static final String DAISHENPI_NUM = "daishenpi_num";// 点击待审批之后数量广播
	public static final String TASK_NUM = "task_num";// 任务点击之后数量广播
	public static final String SELECT_PHOTOS_PIC = "SELECT_PHOTOS_PIC";// 选择图片
	public static final String CAOGAO_NUM = "caogao_num";// 草稿保存广播
	public static final String OPEN_FILE = "open_file";
	public static final String GET_WEIXIN_LOGIN = "getweixinlogin";// 小网页后台定时登陆
	public static final String MESSAGE_RICHENG_EVENT = "message_richeng_event";// 任务事件消息推送
	public static final String EVENT_NUM = "event_num";// 事件消息
	public static final String MESSAGE_CHATMESSAGE = "message_chat";// chat来消息广播
	public static final String MESSAGE_CHAT_NUM = "chat_num";// chat数亮
	public static final String WEB_TO_MENU = "web_to_menu";// 从网页到菜单
	public static final String IM_MESSAGE = "im_message";// 消息广播

	public static String MyWebString = "http://www.cloudcc.com";
	public static String MyWebName = "";
	public static Bitmap topicon = null;
	public static String LOCATION_RECEIVER = "locationreceiver";// 定位广播
	public static boolean isDebug = false;// true打印 ；false不打印
	// 用户信息
	public static String binding = null;// 用于查询
	// 判别是否是登陆
	public static boolean islogin = false;
	// 图片上传的长宽
	public static String ImageWith = "36";
	public static String ImageHeight = "36";
	public static String ImageStyle = "png";
	// 消息数量
	public static int mesnum = 0;
	public static int dspnum = 0;
	public static int rwnum = 0;
	public static int sjnum = 0;
	public static int eventnum = 0;// 消息事件数量
	public static int atnum = 0;
	public static boolean push = false;// 该不该开启服务

	// 判断是不是从菜单点击进去
	public static boolean iscomeMenu;
	public static int position = 3;
	public static boolean mapIsChecked;// 地图中的那个选择是否选中
	public static boolean contaxtIsChecked;// 在发chatter中，这个人是否被选中
	public static String iszan = "";
	public static String shoucang = "";
	public static String toupiao = "";
	public static int isSelectedNum = 0;// 多少个图片已经选择
	public static String filePath = "";// 打开的文件路径
	public static boolean isStopService;// 是否退出bindingsercice
	public static int choiceposition;// 当前选择的相册
	public static boolean isFirst = true;// 留webview错误判断用的
	public static boolean isError;// 留webview错误判断用的
	public static String indexUrl;// 留webview中判别是否要返回
	public static int viewpagerNum = 0;// 添加滑动页码
	// public static int totalNum;// 总共多少页
	public static boolean isCloseAdd = false;// 是否把添加页面finish
	public static boolean isWEbFisrt = true;// 小网页测试是否是第一个网页

	public static boolean isFenXiang = false;// 是不是分享 相册分享图片

	public static Bundle isBundle = null;
	public static String relevantName = "";

	public HashMap<Integer, Integer> contactmap = new HashMap<Integer, Integer>();
	protected NotificationManager mNotificationManager;
	protected Notification mNotification;

	protected static Application mInstance;

	public long lastLoginTime = 0;

	@Override
	public void onCreate() {
		mInstance = this;
		super.onCreate();
		initNotification();
		initImageLoader(mInstance);
		initOther();
	}

	private void initOther() {
		screenH = (int) DensityUtils.getScreenHeight(mInstance);
		screenW = (int) DensityUtils.getScreenWidth(mInstance);
	}



	private void initNotification() {
		mNotificationManager = (NotificationManager) getSystemService(android.content.Context.NOTIFICATION_SERVICE);
		if (false&& Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD) {
			StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
					.detectAll().penaltyDialog().build());
			StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
					.detectAll().penaltyDeath().build());
		}
	}



	public static void initImageLoader(Context context) {
		File cacheDir = StorageUtils.getCacheDirectory(context);
		// File cacheDir =
		// StorageUtils.getOwnCacheDirectory(MyApplication.mInstance,
		// "imageloader/Cache");
		ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(
				context)
				.memoryCacheExtraOptions(480, 800)
				// max width, max height，即保存的每个缓存文件的最大长宽
				.discCacheExtraOptions(480, 800, null)
				// Can slow ImageLoader, use it carefully (Better don't use
				// it)/设置缓存的详细信息，最好不要设置这个
				.threadPoolSize(3)
				// 线程池内加载的数量
				.threadPriority(Thread.NORM_PRIORITY - 2)
				.denyCacheImageMultipleSizesInMemory()
				.memoryCache(new UsingFreqLimitedMemoryCache(2 * 1024 * 1024))
				// You can pass your own memory cache
				// implementation/你可以通过自己的内存缓存实现
				.memoryCacheSize(2 * 1024 * 1024)
				.discCacheSize(50 * 1024 * 1024)
				.discCacheFileNameGenerator(new Md5FileNameGenerator())
				// 将保存的时候的URI名称用MD5 加密
				.tasksProcessingOrder(QueueProcessingType.LIFO)
				.discCacheFileCount(100)
				// 缓存的文件数量
				.discCache(new UnlimitedDiscCache(cacheDir))
				// 自定义缓存路径
				.defaultDisplayImageOptions(DisplayImageOptions.createSimple())
				.imageDownloader(
						new BaseImageDownloader(context, 5 * 1000, 30 * 1000)) // connectTimeout
																				// (5
				// s),
				// readTimeout
				// (30
				// s)超时时间
				.writeDebugLogs() // Remove for release app
				.build();// 开始构建
		ImageLoader.getInstance().init(config);
	}

	/**
	 * 获得applicationContext
	 * 
	 * @return Application
	 */
	public static Application getApp() {
		return mInstance;
	}
	/**
	 * 或扽 崩溃日志上传的id 如果没有将上传到框架默认id
	 * 
	 * @return
	 */
	public String getCrashId() {
		return "900004218";
	}

}
